package com.zjdiepu.www.biz.third.net;

import java.net.SocketTimeoutException;
import java.text.DecimalFormat;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.common.consts.ThirdService;
import com.zjdiepu.www.commpent.HttpClient;
import com.zjdiepu.www.config.JuheServiceProperty;
import com.zjdiepu.www.ibiz.third.net.IUsedCarExactEstimationService;
import com.zjdiepu.www.model.third.request.juhe.JuheExactEstimationRequest;
import com.zjdiepu.www.model.third.response.juhe.JuheExactEstimationResponse;

/**
 * 项目名称：dp-biz <br/> 
 * 类名称：JuheUsedCarExactEstimationServiceImpl <br/>  
 * 类描述： 二手车精确估价:聚合数据<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年3月27日 上午10:12:24  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年3月27日 上午10:12:24   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Service("juheUsedCarExactEstimationServiceImpl")
public class JuheUsedCarExactEstimationServiceImpl extends BaseService implements IUsedCarExactEstimationService<JuheExactEstimationResponse, JuheExactEstimationRequest>{
	private static final Logger logger = LoggerFactory.getLogger(JuheUsedCarExactEstimationServiceImpl.class);
	
	@Resource
	protected HttpClient httpClient;
	@Resource
	private JuheServiceProperty juheServiceProperty;
	@Override
	public JuheExactEstimationResponse exactEstimation(JuheExactEstimationRequest exactEstimationRequest) throws SocketTimeoutException, HttpClientException {
		Map<String, String> juheCommonParamMap = getJuheCommonParamMap(juheServiceProperty.getAppKey());
		JuheExactEstimationRequest juheExactEstimationRequest = (JuheExactEstimationRequest)exactEstimationRequest;
		juheCommonParamMap.put("carstatus", juheExactEstimationRequest.getCarStatus().toString());
		juheCommonParamMap.put("purpose", juheExactEstimationRequest.getPurpose().toString());
		juheCommonParamMap.put("city", juheExactEstimationRequest.getCity().toString());
		juheCommonParamMap.put("province", juheExactEstimationRequest.getProvince().toString());
		juheCommonParamMap.put("car", juheExactEstimationRequest.getCar().toString());
		Integer usedDate = juheExactEstimationRequest.getUsedDate();
		juheCommonParamMap.put("useddate", new DecimalFormat("0000").format(usedDate));
		String usedDateMonth = juheExactEstimationRequest.getUsedDateMonth();
		juheCommonParamMap.put("useddateMonth", new DecimalFormat("00").format(Integer.valueOf(usedDateMonth)));
		juheCommonParamMap.put("mileage", juheExactEstimationRequest.getMileage().toString());
		juheCommonParamMap.put("price",  new DecimalFormat("00.00").format(juheExactEstimationRequest.getPrice()));
		String responseJson = httpClient.get(ThirdService.JUHE_ASSESS, juheCommonParamMap);
		JSONObject jsonObject = (JSONObject) JSONObject.parse(responseJson);
		checkJuheResponse(jsonObject, logger);
		JSONObject resultJSONObject = (JSONObject)jsonObject.get("result");
		return resultJSONObject.toJavaObject(JuheExactEstimationResponse.class);
	}
}
